//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

unit udmtopics;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, ZConnection, ZDataset, udmbase, udmgeneral;

type

  { TdmTopics }

  TdmTopics = class(TdmBase)
    zqAddTopic: TZQuery;
    zqUpdateTopic: TZQuery;
    zroqTopic: TZReadOnlyQuery;
    zroqTopics: TZReadOnlyQuery;
  private
    { private declarations }
  public
    { public declarations }

    function AddTopic( Code:string; Lbl:string; ParentTopic:integer; Description: string): integer;
    function UpdateTopic( id:integer; Code:string; Lbl:string; ParentTopic:integer; Description: string): boolean;
    function GetTopicIdByCode( acode:string):integer;

  end; 

var
  dmTopics: TdmTopics;

const
  NO_PARENT_ID = -1;

implementation

{$R *.lfm}

{ TdmTopics }


//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

function TdmTopics.AddTopic(Code: string; lbl: string; ParentTopic: integer;
  Description: string): integer;
begin
     with zqAddTopic do
     begin
       paramByName('pcode').asString := Code;
       paramByName('plabel').asString := Lbl;
       paramByName('pparent_topic').asInteger := ParentTopic;
       paramByName('pdescription').asString := Description;
       ExecSQL;
       result := fieldByname('id').asInteger;
     end;
end;

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

function TdmTopics.UpdateTopic(id: integer; Code: string; Lbl: string;
  ParentTopic: integer; Description: string): boolean;
begin
     with zqAddTopic do
     begin
       paramByName('pid').asInteger := id;
       paramByName('pcode').asString := Code;
       paramByName('plabel').asString := Lbl;
       paramByName('parent_topic').asInteger := ParentTopic;
       paramByName('pdescription').asString := Description;
       ExecSQL;
    end;
end;

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------

function TdmTopics.GetTopicIdByCode(acode: string): integer;
begin
  try
  with zroqTopic do
  begin
       sql.Text:= 'select id from topics where code ='''' + code + ''';
       ExecSql;
       result := fieldByName('id').asInteger;
  end;
  except
        result := -1;
  end;
end;


end.

